# coding=utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')


class Scorer(object):
    def __init__(self, cheesboard, cheesborad_size, self_agent, other_agent):
        self.cheesboard = cheesboard
        self.cheesboard_size = cheesborad_size
        self.self_agent = self_agent
        self.other_agent = other_agent
        self.FIVE = 0  # 成五
        self.FOUR = 1  # 活四
        self.SFOUR = 2  # 冲四
        self.THREE = 3  # 活三
        self.STHREE = 4  # 冲三
        self.TWO = 5  # 活二
        self.STWO = 6  # 冲二
        self.ONE = 7  # 单子
        self.score_dict = {}

    def check_left_and_right(self, row, col, l_num, r_num, other_agent):
        """
        检查左右两边是否可落子
        :param row: 当前光标行号
        :param col: 当前光标列号
        :param l_num: 左边减数
        :param r_num: 右边加数
        :param other_agent: 对手标识
        :return: 0-两边均不能落子 1-其中一边可以落子 2-两边均可落子
        """
        left_ok = 1
        right_ok = 1
        if col + r_num >= self.cheesboard_size or self.cheesboard[row][col + r_num] == other_agent:
            right_ok = 0
        if col - l_num < 0 or self.cheesboard[row][col - l_num] == other_agent:
            left_ok = 0
        is_ok = left_ok + right_ok
        return is_ok

    def check_up_and_down(self, row, col, u_num, d_num, other_agent):
        """
        检查上下两边是否可落子
        :param row: 当前光标行号
        :param col: 当前光标列号
        :param u_num: 上边减数
        :param d_num: 下边加数
        :param other_agent: 对手标识
        :return:
        """
        left_ok = 1
        right_ok = 1
        if row + d_num >= self.cheesboard_size or self.cheesboard[row + d_num][col] == other_agent:
            right_ok = 0
        if row - u_num < 0 or self.cheesboard[row - u_num][col] == other_agent:
            left_ok = 0
        is_ok = left_ok + right_ok
        return is_ok

    def check_leftup_to_rightdown(self, row, col, lu_num, rd_num, other_agent):
        """
        检查左上和右下两边是否可落子
        :param row: 当前光标行号
        :param col: 当前光标列号
        :param lu_num: 左上减数
        :param rd_num: 右下加数
        :param other_agent: 对手标识
        :return:
        """
        left_ok = 1
        right_ok = 1
        if row + rd_num >= self.cheesboard_size or col + rd_num >= self.cheesboard_size or \
                        self.cheesboard[row + rd_num][col + rd_num] == other_agent:
            right_ok = 0
        if row - lu_num < 0 or col - lu_num < 0 or self.cheesboard[row - lu_num][col - lu_num] == other_agent:
            left_ok = 0
        is_ok = left_ok + right_ok
        return is_ok

    def count_horizontal_score(self, self_agent, other_agent):
        """
        计算水平方向上self_agent的得分
        :param self_agent: 己方标识
        :param other_agent: 对手标识
        :return:
        """
        FIVE = self.FIVE
        FOUR = self.FOUR
        SFOUR = self.SFOUR
        THREE = self.THREE
        STHREE = self.STHREE
        TWO = self.TWO
        STWO = self.STWO
        ONE = self.ONE
        for row in range(self.cheesboard_size):
            col = 0
            # 对于每一行
            while col < self.cheesboard_size:
                if self.cheesboard[row][col] != self_agent:  # 空位，跳过
                    col += 1
                    continue
                # 检查每一列
                else:
                    self_cnt = 0
                    # 统计连续的棋子数
                    while col < self.cheesboard_size and self.cheesboard[row][col] == self_agent:
                        self_cnt += 1
                        col += 1
                    # 连续五子
                    if self_cnt >= 5:
                        self.score_dict[self_agent][FIVE] += 1
                    # 连续四子
                    elif self_cnt == 4:
                        # 检查两端，是否活4
                        is_ok = self.check_left_and_right(row, col, 5, 0, other_agent)
                        # 两端堵死，无用棋
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        # 一端堵死，冲四
                        elif is_ok == 1:
                            self.score_dict[self_agent][SFOUR] += 1
                        # 活四
                        else:
                            self.score_dict[self_agent][FOUR] += 1
                    # 连续3子
                    elif self_cnt == 3:
                        # 检查是否能形成跳四
                        if col + 1 < self.cheesboard_size and self.cheesboard[row][col] == 0 and self.cheesboard[row][
                                    col + 1] == self_agent:  # 检查右边是否还能落2子，并且形成跳四
                            self.score_dict[self_agent][SFOUR] += 1
                            # 列标记后移两格
                            col += 2
                            continue
                        # 检查左右两端
                        is_ok = self.check_left_and_right(row, col, 4, 0, other_agent)
                        if is_ok == 0:  # 两端堵死
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:  # 一端堵死
                            self.score_dict[self_agent][STHREE] += 1
                        else:  # 活三
                            self.score_dict[self_agent][THREE] += 1
                    # 连续两子
                    elif self_cnt == 2:
                        # 检查是否形成跳四
                        if col + 2 < self.cheesboard_size and self.cheesboard[row][col] == 0 and self.cheesboard[row][
                                    col + 1] == self_agent and self.cheesboard[row][col + 2] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            col += 3
                            continue
                        # 检查是否形成跳三
                        if col + 1 < self.cheesboard_size and self.cheesboard[row][col] == 0 and self.cheesboard[row][
                                    col + 1] == self_agent:
                            is_ok = self.check_left_and_right(row, col, 3, 2, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            elif is_ok == 2:
                                self.score_dict[self_agent][THREE] += 1
                            col += 2
                            continue
                        # 检查是否活二
                        is_ok = self.check_left_and_right(row, col, 3, 0, other_agent)
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:
                            self.score_dict[self_agent][STWO] += 1
                        else:
                            self.score_dict[self_agent][TWO] += 1
                    elif self_cnt == 1:
                        # 检查是否形成跳四
                        if col + 3 < self.cheesboard_size and self.cheesboard[row][col] == 0 and self.cheesboard[row][
                                    col + 1] == self_agent and self.cheesboard[row][col + 2] == self_agent and \
                                        self.cheesboard[row][col + 3] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            col += 4
                            continue
                        # 检查是否形成跳三
                        if col + 2 < self.cheesboard_size and self.cheesboard[row][col] == 0 and self.cheesboard[row][
                                    col + 1] == self_agent and self.cheesboard[row][col + 2] == self_agent:
                            is_ok = self.check_left_and_right(row, col, 2, 3, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            else:
                                self.score_dict[self_agent][THREE] += 1
                            col += 3
                            continue
                        self.score_dict[self_agent][ONE] += 1

    def count_vertical_score(self, self_agent, other_agent):
        """
        计算垂直方向上self_agent的得分
        :param self_agent: 己方标识
        :param other_agent: 对手标识
        :return:
        """
        FIVE = self.FIVE
        FOUR = self.FOUR
        SFOUR = self.SFOUR
        THREE = self.THREE
        STHREE = self.STHREE
        TWO = self.TWO
        STWO = self.STWO
        ONE = self.ONE
        for col in range(self.cheesboard_size):
            row = 0
            # 对于每一列
            while row < self.cheesboard_size:
                if self.cheesboard[row][col] != self_agent:  # 空位，跳过
                    row += 1
                    continue
                # 检查每一列
                else:
                    self_cnt = 0
                    # 统计连续的棋子数
                    while row < self.cheesboard_size and self.cheesboard[row][col] == self_agent:
                        self_cnt += 1
                        row += 1
                    # 连续五子
                    if self_cnt >= 5:
                        self.score_dict[self_agent][FIVE] += 1
                    # 连续四子
                    elif self_cnt == 4:
                        # 检查两端，是否活4
                        is_ok = self.check_up_and_down(row, col, 5, 0, other_agent)
                        # 两端堵死，无用棋
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        # 一端堵死，冲四
                        elif is_ok == 1:
                            self.score_dict[self_agent][SFOUR] += 1
                        # 活四
                        else:
                            self.score_dict[self_agent][FOUR] += 1
                    # 连续3子
                    elif self_cnt == 3:
                        # 检查是否能形成跳四
                        if row + 1 < self.cheesboard_size and self.cheesboard[row][col] == 0 and \
                                        self.cheesboard[row + 1][
                                            col] == self_agent:  # 检查下边是否还能落2子，并且形成跳四
                            self.score_dict[self_agent][SFOUR] += 1
                            # 列标记后移两格
                            row += 2
                            continue
                        # 检查左右两端
                        is_ok = self.check_up_and_down(row, col, 4, 0, other_agent)
                        if is_ok == 0:  # 两端堵死
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:  # 一端堵死
                            self.score_dict[self_agent][STHREE] += 1
                        else:  # 活三
                            self.score_dict[self_agent][THREE] += 1
                    # 连续两子
                    elif self_cnt == 2:
                        # 检查是否形成跳四
                        if row + 2 < self.cheesboard_size and self.cheesboard[row][col] == 0 and \
                                        self.cheesboard[row + 1][
                                            col] == self_agent and self.cheesboard[row + 2][col] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            row += 3
                            continue
                        # 检查是否形成跳三
                        if row + 1 < self.cheesboard_size and self.cheesboard[row][col] == 0 and \
                                        self.cheesboard[row + 1][
                                            col] == self_agent:
                            is_ok = self.check_up_and_down(row, col, 3, 2, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            elif is_ok == 2:
                                self.score_dict[self_agent][THREE] += 1
                            row += 2
                            continue
                        # 检查是否活二
                        is_ok = self.check_up_and_down(row, col, 3, 0, other_agent)
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:
                            self.score_dict[self_agent][STWO] += 1
                        else:
                            self.score_dict[self_agent][TWO] += 1
                    elif self_cnt == 1:
                        # 检查是否形成跳四
                        if row + 3 < self.cheesboard_size and self.cheesboard[row][col] == 0 and \
                                        self.cheesboard[row + 1][
                                            col] == self_agent and self.cheesboard[row + 2][col] == self_agent and \
                                        self.cheesboard[row + 3][col] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            row += 4
                            continue
                        # 检查是否形成跳三
                        if row + 2 < self.cheesboard_size and self.cheesboard[row][col] == 0 and \
                                        self.cheesboard[row + 1][
                                            col] == self_agent and self.cheesboard[row + 2][col] == self_agent:
                            is_ok = self.check_up_and_down(row, col, 2, 3, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            else:
                                self.score_dict[self_agent][THREE] += 1
                            row += 3
                            continue
                        self.score_dict[self_agent][ONE] += 1

    def count_left_to_right_score(self, self_agent, other_agent):
        """
        计算从左到右斜方向上self_agent的得分
        :param self_agent: 己方标识
        :param other_agent: 对手标识
        :return:
        """
        FIVE = self.FIVE
        FOUR = self.FOUR
        SFOUR = self.SFOUR
        THREE = self.THREE
        STHREE = self.STHREE
        TWO = self.TWO
        STWO = self.STWO
        ONE = self.ONE
        row_col_list = []
        for row in range(self.cheesboard_size):
            row_col_list.append((row, 0))
        for col in range(1, self.cheesboard_size):
            row_col_list.append((0, col))
        for row, col in row_col_list:
            while row < self.cheesboard_size and col < self.cheesboard_size:
                if self.cheesboard[row][col] != self_agent:  # 空位，跳过
                    row += 1
                    col += 1
                    continue
                # 检查每一列
                else:
                    self_cnt = 0
                    # 统计连续的棋子数
                    while col < self.cheesboard_size and row < self.cheesboard_size and self.cheesboard[row][
                        col] == self_agent:
                        self_cnt += 1
                        col += 1
                        row += 1
                    # 连续五子
                    if self_cnt >= 5:
                        self.score_dict[self_agent][FIVE] += 1
                    # 连续四子
                    elif self_cnt == 4:
                        # 检查两端，是否活4
                        is_ok = self.check_leftup_to_rightdown(row, col, 5, 0, other_agent)
                        # 两端堵死，无用棋
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        # 一端堵死，冲四
                        elif is_ok == 1:
                            self.score_dict[self_agent][SFOUR] += 1
                        # 活四
                        else:
                            self.score_dict[self_agent][FOUR] += 1
                    # 连续3子
                    elif self_cnt == 3:
                        # 检查是否能形成跳四
                        if col + 1 < self.cheesboard_size and row + 1 < self.cheesboard_size and self.cheesboard[row][
                            col] == 0 and self.cheesboard[row + 1][
                                    col + 1] == self_agent:  # 检查右边是否还能落2子，并且形成跳四
                            self.score_dict[self_agent][SFOUR] += 1
                            # 列标记后移两格
                            col += 2
                            row += 2
                            continue
                        # 检查左右两端
                        is_ok = self.check_leftup_to_rightdown(row, col, 4, 0, other_agent)
                        if is_ok == 0:  # 两端堵死
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:  # 一端堵死
                            self.score_dict[self_agent][STHREE] += 1
                        else:  # 活三
                            self.score_dict[self_agent][THREE] += 1
                    # 连续两子
                    elif self_cnt == 2:
                        # 检查是否形成跳四
                        if col + 2 < self.cheesboard_size and row + 2 < self.cheesboard_size and self.cheesboard[row][
                            col] == 0 and self.cheesboard[row + 1][
                                    col + 1] == self_agent and self.cheesboard[row + 2][col + 2] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            col += 3
                            row += 3
                            continue
                        # 检查是否形成跳三
                        if col + 1 < self.cheesboard_size and row + 1 < self.cheesboard_size and self.cheesboard[row][
                            col] == 0 and self.cheesboard[row + 1][
                                    col + 1] == self_agent:
                            is_ok = self.check_leftup_to_rightdown(row, col, 3, 2, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            elif is_ok == 2:
                                self.score_dict[self_agent][THREE] += 1
                            col += 2
                            row += 2
                            continue
                        # 检查是否活二
                        is_ok = self.check_leftup_to_rightdown(row, col, 3, 0, other_agent)
                        if is_ok == 0:
                            self.score_dict[self_agent][ONE] += 1
                        elif is_ok == 1:
                            self.score_dict[self_agent][STWO] += 1
                        else:
                            self.score_dict[self_agent][TWO] += 1
                    elif self_cnt == 1:
                        # 检查是否形成跳四
                        if col + 3 < self.cheesboard_size and row + 3 < self.cheesboard_size and self.cheesboard[row][
                            col] == 0 and self.cheesboard[row + 1][
                                    col + 1] == self_agent and self.cheesboard[row + 2][col + 2] == self_agent and \
                                        self.cheesboard[row + 3][col + 3] == self_agent:
                            self.score_dict[self_agent][SFOUR] += 1
                            col += 4
                            row += 4
                            continue
                        # 检查是否形成跳三
                        if col + 2 < self.cheesboard_size and row + 2 < self.cheesboard_size and self.cheesboard[row][
                            col] == 0 and self.cheesboard[row + 1][
                                    col + 1] == self_agent and self.cheesboard[row + 2][col + 2] == self_agent:
                            is_ok = self.check_leftup_to_rightdown(row, col, 2, 3, other_agent)
                            if is_ok == 0:
                                self.score_dict[self_agent][ONE] += 1
                            elif is_ok == 1:
                                self.score_dict[self_agent][STHREE] += 1
                            else:
                                self.score_dict[self_agent][THREE] += 1
                            col += 3
                            row += 3
                            continue
                        self.score_dict[self_agent][ONE] += 1

    def count_socre(self, self_agent, other_agent):
        """
        统计当前局势上，主体的得分
        :param self_agent: 棋盘主体
        :param other_agent: 棋盘对手
        :return:计算出来的最终分数
        """
        FIVE = self.FIVE
        FOUR = self.FOUR
        SFOUR = self.SFOUR
        THREE = self.THREE
        STHREE = self.STHREE
        TWO = self.TWO
        STWO = self.STWO
        ONE = self.ONE
        self.score_dict[self_agent] = [0 for i in range(8)]
        self.score_dict[other_agent] = [0 for i in range(8)]

        # 横向统计
        self.count_horizontal_score(self_agent, other_agent)  # 计算己方分数
        self.count_horizontal_score(other_agent, self_agent)  # 计算对手分数

        # 垂直统计
        self.count_vertical_score(self_agent, other_agent)  # 计算己方分数
        self.count_vertical_score(other_agent, self_agent)  # 计算对手分数

        # 从左到右斜方向
        self.count_left_to_right_score(self_agent, other_agent)
        self.count_left_to_right_score(other_agent, self_agent)

        # 从右到左斜方向
